<template>
  <div class="person">
    <h2>TsDemo</h2>
  </div>
</template>

<script lang="ts" setup name="TsAdvanced">
// 类class
class Animal {
  public name: string;
  constructor(name: string) {
    this.name = name;
  }
  sayHi(): string {
    return `My name is ${this.name}`;
  }
}
let animal = new Animal("cat");
console.log('类class animal', animal);


// 交叉类型 表示一个值的类型是几种类型的并集(集合)。有点类似于继承。
interface Dog {
  name: string;
  num: number;
}
interface Cat {
  name: string;
  age: number;
}
type J = Dog & Cat;
let a: J = {
  name: "aaa",
  num: 1,
  age: 2
};
console.log('交叉类型', a)
// 泛型 在程序中定义形式类型参数，然后在泛型实例化时使用实际类型参数来替换形式类型参数
function test<T>(arg: T): T {
  return arg;
}
let output = test<string>("Hello");
let output1 = test<number>(6);
console.log(output, output1); // 输出: Hello 6

function reverse<T>(array: T[]): T[] {
  return array.reverse();
}
let numbers: number[] = [1, 2, 3, 4, 5];
let reversedNumbers = reverse(numbers);
console.log(reversedNumbers); // 输出：[5, 4, 3, 2, 1]
let strings: string[] = ["apple", "banana", "orange"];
let reversedStrings = reverse(strings);
console.log(reversedStrings); // 输出：["orange", "banana", "apple"]

</script>